package org.dendy.browser;

import org.dendy.browser.menubar.OpenMenuBar;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Class ini menampung dari isi Browser Seperti Browsernya sendiri,composite dan
 * Button pada toolbar
 * 
 * @author dendy
 * 
 */
public class ManagerBrowser {

	Shell shell = null;

	Composite controlPosition = null;

	FormData position = null;

	public static Browser browser = null;

	public ManagerBrowser(Shell shell) {
		this.shell = shell;
		shell.setLayout(new FormLayout());
	}

	/**
	 * membuat composit dan browser
	 * @param display
	 */
	void createContents(Display display) {
		shell.setLayout(new FormLayout());

		// Create Composite to hold button and text field
		controlPosition = new Composite(shell, SWT.NONE);
		position = new FormData();
		position.top = new FormAttachment(0, 0);
		position.left = new FormAttachment(0, 0);
		position.right = new FormAttachment(100, 0);
		controlPosition.setLayoutData(position);

		// Create Browser
		browser = new Browser(shell, SWT.NONE);
		position = new FormData();
		position.top = new FormAttachment(controlPosition);
		position.bottom = new FormAttachment(100, 0);
		position.left = new FormAttachment(0, 0);
		position.right = new FormAttachment(100, 0);
		browser.setLayoutData(position);

		// Create Control
		controlPosition.setLayout(new GridLayout(5, false));

		// Create Button
		createButton(display);

		// MenuBar
		OpenMenuBar bar = new OpenMenuBar(shell);
		bar.createMenuBarMain();
	}

	/**
	 * Method yang berfungsi untuk membuat button pada browser
	 * 
	 * @param display
	 */
	void createButton(Display display) {
		Image back = new Image(display, "images/001_23.png");
		Image forward = new Image(display, "images/001_21.png");
		Image refresh = new Image(display, "images/001_39.png");
		Image go = new Image(display, "images/001_40.png");

		// Create back button
		Button button = new Button(controlPosition, SWT.PUSH);
		button.setImage(back);
		button.setToolTipText("Go back to page");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				browser.back();
			}
		});

		// Create forward button
		button = new Button(controlPosition, SWT.PUSH);
		button.setImage(forward);
		button.setToolTipText("Go forward to page");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				browser.forward();
			}
		});

		// Create refresh button
		button = new Button(controlPosition, SWT.PUSH);
		button.setImage(refresh);
		button.setToolTipText("refresh page");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				browser.refresh();
			}
		});

		/** Membuat text agar bisa menampilkan url-nya */
		final Text url = new Text(controlPosition, SWT.BORDER);
		url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		url.setText("about:blank");
		url.setFocus();

		// Create go button
		button = new Button(controlPosition, SWT.PUSH);
		button.setImage(go);
		button.setToolTipText("Go");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				browser.setUrl(url.getText());
			}
		});

		shell.setDefaultButton(button);
	}
}
